package todo;


import done.AbstractWashingMachine;
import se.lth.cs.realtime.*;
import se.lth.cs.realtime.event.RTEvent;


public class TemperatureController extends PeriodicThread {
    // TODO: add suitable attributes
    public static final int TEMP_IDLE  = 0;
    public static final int TEMP_SET   = 1;
    private boolean on;
    int mode;
    boolean ack;
    boolean start;
    TemperatureEvent e;
    TemperatureEvent tmp;
	
	private double temp;
	AbstractWashingMachine theMachine;

    public TemperatureController(AbstractWashingMachine theMachine, long speed) {
	super(speed); // TODO: replace with suitable period
	on = false;
	this.theMachine = theMachine;
	temp = 0;
	mode = TemperatureEvent.TEMP_IDLE;
	ack = false;
    }

    public void perform() {
    	tmp = (TemperatureEvent) mailbox.tryFetch();
    	if(tmp!= null){
    		e = tmp;
    		temp = e.getTemperature();
    		mode = e.getMode();
    		ack = true;
    		
    	}
    		
    			if(mode == TemperatureEvent.TEMP_SET){
    				
    				if(theMachine.getWaterLevel() > 0.1){
    				
    				if(theMachine.getTemperature() >= temp-1){
    					theMachine.setHeating(false);
    					
    					if(ack){
    						ack = false;
    						((WashingProgram)e.getSource()).putEvent(new AckEvent(this));
    						
    					}
    					
    				}
    				if(theMachine.getTemperature() <= temp-1.8){
    					theMachine.setHeating(true);
    				}
    				
    				}
    				
    			}
    			
    			if(mode == TemperatureEvent.TEMP_IDLE&&ack){
    				theMachine.setHeating(false);
					//((WashingProgram)e.getSource()).putEvent(new AckEvent(this));
					ack = false;
    			}
		    	
    	
    	
    		
    		
    		
    		
    	
    }
}
























